(function () {
    "use strict";

    angular.module('core').controller('reportOperacaoVV010203Controller', controller);
    controller.$inject = ['$scope', '$alert', 'toolsService', 'pedidoVarejoService'];

    function controller($scope, $alert, toolsService, pedidoVarejoService) {
        $scope.initializeLayout('Análise de Pagamentos Pedidos de Varejo');

        //[Inicializa objetos]
        $scope.state = {
            data: [],
            carregando: false,
            vazio: false,
            count: 0,
            filtro: {
                ativo: false,
                args: null
            }
        };

        //[Inicializa funcões]
        $scope.initialize = function () {
            $scope.clearFilter();
            $scope.refresh();
        };

        $scope.getAll = function () {
            $scope.state.filtro.args.skip = 0;
            $scope.state.carregando = true;
            $scope.state.vazio = false;
            pedidoVarejoService
                .getAllPagamento($scope.state.filtro.args)
                .then(
                    function (response) {
                        if (response.Status < 0) {
                            $alert({ text: response.Message, title: $scope.Title });
                        } else {
                            angular.copy(angular.fromJson(response.Data), $scope.state.data);

                            /* Início Pivot Table */
                            var dateFormat = $.pivotUtilities.derivers.dateFormat;
                            var sortAs = $.pivotUtilities.sortAs;
                            var tpl = $.pivotUtilities.aggregatorTemplates;
                            var fmt = $.pivotUtilities.numberFormat({ suffix: " °C" });

                            $("#pivot-table").pivotUI(toolsService.flattenArray(response.Data), {
                                hiddenAttributes: [],
                                derivedAttributes: {
                                    "PedidoVarejo.Emissao.Ano": dateFormat("PedidoVarejo.Emissao", "%y", true),
                                    "PedidoVarejo.Emissao.Mes": dateFormat("PedidoVarejo.Emissao", "%n", true),
                                    "PedidoVarejo.Emissao.Dia": dateFormat("PedidoVarejo.Emissao", "%d", true),
                                    "PedidoVarejo.Emissao.DiaSemana": dateFormat("PedidoVarejo.Emissao", "%w", true),
                                },
                                sorters: {
                                    "PedidoVarejo.Emissao.Mes": sortAs(["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]),
                                    "PedidoVarejo.Emissao.DiaSemana": sortAs(["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]),
                                },
                                rows: ["PedidoVarejo.Filial.Empresa.Nome", "Tipo"],
                                cols: ["PedidoVarejo.Emissao.Ano", "PedidoVarejo.Emissao.Mes"],
                                vals: ["Valor"],
                                aggregatorName: "Sum",
                                rendererName: "Heatmap",
                                renderers: $.extend(
                                    $.pivotUtilities.renderers,
                                    $.pivotUtilities.c3_renderers,
                                    $.pivotUtilities.d3_renderers
                                ),
                            });

                            $scope.tools.closeColunasDinamicas();

                            /* Fim Pivot Table */

                        }
                        $scope.state.carregando = false;
                        $scope.state.vazio = ($scope.state.data == 0);
                    },
                    function (_error) {
                        $scope.state.carregando = false;
                        $scope.tools.toastr.error('Não foi possível conectar com o(s) servidor(es)');
                    }
                );
        };
        $scope.refresh = function () {
            $scope.getAll();
        };

        $scope.clearFilter = function () {
            $scope.state.filtro.args = {
                chaves: '',
                filtro: {
                    PedidoVarejo: {
                        Status: 1
                    }
                },
                filtroDe:
                {
                    PedidoVarejo: {
                        Emissao: $scope.tools.getHoje()
                    }
                },
                filtroAte: null,
                sortExpression: '',
                skip: 0,
                take: 999999
            };
        };
        $scope.toggleFilter = function () {
            $scope.state.filtro.ativo = !$scope.state.filtro.ativo;
        };

        /*Inicializa funções*/
        $scope.initialize();

    }

})();